<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item :label="$t('订单号')">
              <a-input :placeholder="$t('请输入订单号')" v-model="queryParam.orderId"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="10" :lg="11" :md="12" :sm="24">
            <a-form-item :label="$t('购买时间')">
              <j-date
                :show-time="{ defaultValue: moment('00:00:00', 'HH:mm:ss') }"
                valueFormat="YYYY-MM-DD HH:mm:ss"
                dateFormat="YYYY-MM-DD HH:mm:ss"
                :placeholder="$t('请选择开始时间')"
                class="query-group-cust"
                v-model="queryParam.buyTime_begin"
                style="min-width: calc(50% - 10px)"
              ></j-date>
              <span class="query-group-split-cust"></span>
              <j-date
                :show-time="{ defaultValue: moment('23:59:59', 'HH:mm:ss') }"
                valueFormat="YYYY-MM-DD HH:mm:ss"
                dateFormat="YYYY-MM-DD HH:mm:ss"
                :placeholder="$t('请选择结束时间')"
                class="query-group-cust"
                v-model="queryParam.buyTime_end"
                style="min-width: calc(50% - 10px)"
              ></j-date>
            </a-form-item>
          </a-col>
          <template v-if="toggleSearchStatus">
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item :label="$t('商品编码')">
                <a-input :placeholder="$t('请输入商品编码')" v-model="queryParam.goodsId"></a-input>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item :label="$t('商品名称')">
                <a-input :placeholder="$t('请输入商品名称')" v-model="queryParam.goodsName"></a-input>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item :label="$t('o_分类名称')">
                <a-tree-select
                  allowClear
                  v-model="queryParam.goodsKindId"
                  :dropdownStyle="{ maxHeight: '400px', overflow: 'auto' }"
                  :tree-data="treeData"
                  :placeholder="$t('请选择分类名称')"
                />
              </a-form-item>
            </a-col>
            <a-col :xl="10" :lg="11" :md="12" :sm="24">
              <a-form-item :label="$t('销售单价')">
                <a-input
                  :placeholder="$t('请输入最小值')"
                  class="query-group-cust"
                  v-model="queryParam.price_begin"
                ></a-input>
                <span class="query-group-split-cust"></span>
                <a-input
                  :placeholder="$t('请输入最大值')"
                  class="query-group-cust"
                  v-model="queryParam.price_end"
                ></a-input>
              </a-form-item>
            </a-col>
            <a-col :xl="10" :lg="11" :md="12" :sm="24">
              <a-form-item :label="$t('总金额')">
                <a-input
                  :placeholder="$t('请输入最小值')"
                  class="query-group-cust"
                  v-model="queryParam.total_begin"
                ></a-input>
                <span class="query-group-split-cust"></span>
                <a-input
                  :placeholder="$t('请输入最大值')"
                  class="query-group-cust"
                  v-model="queryParam.total_end"
                ></a-input>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24" v-if="!tenantid">
              <a-form-item :label="$t('门店')">
                <a-select v-model="queryParam.yardCode">
                  <a-select-option v-for="(item, i) in shops" :key="i" :value="item.value"
                    >{{ item.title }}<span v-if="item.value != ''">（{{ item.value }}）</span></a-select-option
                  >
                </a-select>
              </a-form-item>
            </a-col>
          </template>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">{{ $t('查询') }}</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">{{
                $t('重置')
              }}</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? $t('收起') : $t('展开') }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'" />
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator"></div>

    <!-- table区域-begin -->
    <div>
      <a-table
        ref="table"
        size="middle"
        :scroll="{ x: true }"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        class="j-table-force-nowrap"
        @change="handleTableChange"
      >
        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px; font-style: italic">{{ $t('无图片') }}</span>
          <img
            v-else
            :src="getImgView(text)"
            height="25px"
            alt=""
            style="max-width: 80px; font-size: 12px; font-style: italic"
          />
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px; font-style: italic">{{ $t('无文件') }}</span>
          <a-button v-else :ghost="true" type="primary" icon="download" size="small" @click="downloadFile(text)">
            {{ $t('下载') }}
          </a-button>
        </template>
        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">{{ $t('编辑') }}</a>
          <a-divider type="vertical" />
          <a-dropdown>
            <a class="ant-dropdown-link">{{ $t('更多') }}<a-icon type="down" /></a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a @click="handleDetail(record)">{{ $t('详情') }}</a>
              </a-menu-item>
              <a-menu-item>
                <a-popconfirm :title="$t('确定删除吗?')" @confirm="() => handleDelete(record.id)">
                  <a>{{ $t('删除') }}</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>
        <template slot="footer">
          <div style="font-weight: bold">{{ totalInfo }}</div>
        </template>
      </a-table>
    </div>
  </a-card>
</template>

<script>
import Vue from 'vue'
import moment from 'moment'
import '@/assets/less/TableExpand.less'
import { getAction } from '@/api/manage'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { initDictOptions } from '@/components/dict/JDictSelectUtil'

export default {
  name: 'OrderGoodsList',
  mixins: [JeecgListMixin, mixinDevice],
  components: {},
  data() {
    return {
      description: '兑换明细页面',
      moment,
      // 表头
      columns: [
        {
          title: this.$t('订单号'),
          align: 'center',
          dataIndex: 'orderId',
        },
        {
          title: this.$t('购买时间'),
          align: 'center',
          dataIndex: 'buyTime',
        },
        {
          title: this.$t('商品编码'),
          align: 'center',
          dataIndex: 'goodsId',
        },
        {
          title: this.$t('商品名称'),
          align: 'center',
          dataIndex: 'goodsName',
        },
        {
          title: this.$t('o_分类名称'),
          align: 'center',
          dataIndex: 'goodsKindName',
        },
        {
          title: this.$t('商品渠道'),
          align: 'center',
          dataIndex: 'species_dictText',
        },
        {
          title: this.$t('数量'),
          align: 'right',
          dataIndex: 'number',
          customRender: (text, r) => {
            text = (text ==null || text =="" || text ==undefined)?0:parseFloat(text);
            return text.toFixed(0);
          }
        },
        {
          title: this.$t('销售单价'),
          align: 'right',
          dataIndex: 'cashPrice',
          customRender: (text, r) => {
            text = (text ==null || text =="" || text ==undefined)?0:parseFloat(text);
            return text.toFixed(2);
          }
        },
        {
          title: this.$t('总金额'),
          align: 'right',
          dataIndex: 'totalCashPrice',
          customRender: (text, r) => {
            text = (text ==null || text =="" || text ==undefined)?0:parseFloat(text);
            return text.toFixed(2);
          }
        },
        {
          title: this.$t('兑换单价'),
          align: 'right',
          dataIndex: 'price',
          customRender: (text, r) => {
            text = (text ==null || text =="" || text ==undefined)?0:parseFloat(text);
            return text.toFixed(2);
          }
        },
        {
          title: this.$t('总兑换值'),
          align: 'right',
          dataIndex: 'total',
          customRender: (text, r) => {
            text = (text ==null || text =="" || text ==undefined)?0:parseFloat(text);
            return text.toFixed(2);
          }
        },
      ],
      url: {
        list: '/gift/orderGoods',
      },
      treeData: [],
      totalCallback: this.totalCallbacks,
      shops: [],
      tenantid: Vue.ls.get('TENANT_ID'),
      queryParam: {
        yardCode: '',
      },
      totalInfo:''
    }
  },
  created() {
    this.select()
  },
  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    },
  },
  methods: {
    initDictConfig() {
      if (!this.tenantid) {
        this.columns.unshift({
          title: this.$t('门店'),
          align: 'left',
          dataIndex: 'yardCode',
          customRender: (t, r) => {
            let str
            if (t == 0) {
              str = this.$t('总部')
            } else {
              str = r.yardName ? t + ' ' + r.yardName : t
            }
            return str
          },
        })
        initDictOptions('bis_shop,name,code').then((res) => {
          if (res.success) {
            this.shops = res.result
            this.shops.unshift({ value: '', title: this.$t('全部') })
          }
        })
      }
    },
    select() {
      let that = this
      let param = {}
      param.pageNo = 1
      param.pageSize = 9999
      param.column = 'sort'
      param.order = 'asc'
      getAction('/gift/goodsKind', param).then((res) => {
        if (res.success) {
          if (res.result.records.length > 0) {
            res.result.records.forEach((col) => {
              col.key = '' + col.id
              col.value = '' + col.id
              col.title = '' + col.name
              col.id = '' + col.id
              col.pid = '' + col.pid
              col.hasChild = '' + col.hasChild
              if (col.hasChild != '0') {
                that.getHasChild(col)
              }
            })
            that.treeData = that.getDataByResult(res.result.records)
          } else {
            that.treeData = []
          }
        } else {
          that.$message.warning(res.message)
        }
      })
    },
    getHasChild(record) {
      let that = this
      let query = {}
      query.pageNo = 1
      query.pageSize = 9999
      query.pid = record.id
      getAction('/gift/goodsKind', query).then((res) => {
        if (res.success) {
          if (res.result.records.length > 0) {
            res.result.records.forEach((col) => {
              col.key = '' + col.id
              col.value = '' + col.id
              col.title = '' + col.name
              col.pid = '' + col.pid
              col.id = '' + col.id
              col.hasChild = '' + col.hasChild
              if (col.hasChild != '0') {
                that.getHasChild(col)
              }
            })
            record.children = that.getDataByResult(res.result.records)
            that.treeData = [...that.treeData]
          }
        } else {
          that.$message.warning(res.message)
        }
      })
    },
    getDataByResult(result) {
      return result.map((item) => {
        return item
      })
    },
    totalCallbacks(totalInfo) {
      if (this.dataSource.length == 0) {
        this.totalInfo = this.$t('合计') + '：--'
        return
      }
      //合计
      this.totalInfo =
        this.$t('合计') +
        '：' +
        this.$t('总金额') +
        '：' +
        totalInfo.totalCashPrice.toFixed(2) +
        '，' +
        this.$t('总兑换值') +
        '：' +
        totalInfo.total.toFixed(2)
    },
    searchReset(){
      this.queryParam={
        yardCode:''
      }
      this.searchQuery()
    }
  },
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>